package leetcode.unionfindset;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Lc399 {

    public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {

        int idx = 0;
        HashMap<String, Integer> index = new HashMap<>();
        double[] ans = new double[queries.size()];
        UnionFindSet unionFindSet = new UnionFindSet(equations.size());
        for (int i = 0; i < equations.size(); i++) {
            String a = equations.get(i).get(0);
            String b = equations.get(i).get(1);
            int ai = index.getOrDefault(a, idx++);
            int bi = index.getOrDefault(b, idx++);
            unionFindSet.union(ai, bi, values[i]);
            index.put(a, ai);
            index.put(b, bi);
        }
        for (int i = 0; i < queries.size(); i++) {
            int a = index.getOrDefault(queries.get(i).get(0), -1);
            int b = index.getOrDefault(queries.get(i).get(1), -1);
            if (a == -1 || b == -1) {
                ans[i] = -1.0;
            } else ans[i] = unionFindSet.getAns(a, b);
        }
        return ans;
    }
    class UnionFindSet {

        int[] parent;
        double[] value;

        public UnionFindSet(int size) {
            this.parent = new int[size * 2];
            this.value = new double[size * 2];
            for (int i = 0; i < parent.length; i++) {
                parent[i] = i;
                value[i] = 1; //到父节点的权值为1，自己是自己的父亲
            }
        }

        public int find(int x) {
            if (x != parent[x]) {
                int oprant = parent[x];
                parent[x] = find(parent[x]);
                value[x] *= value[oprant];
            }
            return parent[x];
        }

        public void union(int x, int y, double z) {
            int pX = find(x);
            int pY = find(y);
            if (pX != pY) {
                parent[pX] = pY;
                value[pX] = z * value[y] / value[x];
            }

        }

        public double getAns(int a, int b) {
            int ap = find(a);
            int bp = find(b);
            if (ap != bp) return -1.0;
            return value[a] / value[b];
        }
    }


    public static void main(String[] args) {
        List<List<String>> l = new ArrayList<>();
        List<String> l1 = new ArrayList<>();
        l1.add("a");
        l1.add("b");
        List<String> l2 = new ArrayList<>();
        l2.add("e");
        l2.add("f");
        List<String> l3 = new ArrayList<>();
        l3.add("b");
        l3.add("e");
        l.add(l1);
        l.add(l2);
        l.add(l3);
        double[] val = new double[]{3.4, 1.4, 2.3};
        List<List<String>> q = new ArrayList<>();
        List<String> q1 = new ArrayList<>();
        q1.add("b");
        q1.add("a");
        List<String> q2 = new ArrayList<>();
        q2.add("a");
        q2.add("f");
        q.add(q1);
        q.add(q2);

        Lc399 lc399 = new Lc399();
        double[] ans = lc399.calcEquation(l,val,q);
        for (int i = 0; i < ans.length; i++) {
            System.out.println(ans[i]);
        }
    }


}
